﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class RolePermissionConfiguration : IEntityTypeConfiguration<RolePermission>
    {
        public void Configure(EntityTypeBuilder<RolePermission> builder)
        {
            builder.ToTable("role_permissions");

            builder.HasKey(rp => new { rp.RoleId, rp.PermissionId }); // ✅复合主键

            builder.Property(rp => rp.RoleId)
                   .HasMaxLength(36)
                   .IsRequired();

            builder.Property(rp => rp.PermissionId)
                   .HasMaxLength(36)
                   .IsRequired();

            builder.Property(rp => rp.IsDeleted)
                   .HasDefaultValue(false); // ✅软删除

            builder.HasOne(rp => rp.Role)
                   .WithMany(r => r.RolePermissions)
                   .HasForeignKey(rp => rp.RoleId)
                   .OnDelete(DeleteBehavior.Restrict); // ✅避免级联

            builder.HasOne(rp => rp.Permission)
                   .WithMany(p => p.RolePermissions)     // ✅补全集合导航
                   .HasForeignKey(rp => rp.PermissionId)
                   .OnDelete(DeleteBehavior.Restrict);

        }
    }
    
}
